<template>
    <a-modal
            :title="modalTitle"
            :width="800"
            :visible="visible"
            :confirmLoading="confirmLoading"
                    @cancel="handleCancel"
    >
        <a-form :form="form">
            <row-list :col="1" v-show="false">
                <row-item>
                    <a-form-item>
                        <a-input v-decorator="['id']" type="hidden"/>
                    </a-form-item>
                </row-item>
            </row-list>
                            <row-list :col="1">
                                                                                                                    <row-item>
                                <a-form-item
                                        label="所属工作票执行单"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['ticketExcuteId', {rules: [{required: true, message: '所属工作票执行单不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>
                                <a-form-item
                                        label="安措类别"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['type', {rules: [{required: true, message: '安措类别不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>
                                <a-form-item
                                        label="安措名称"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['name', {rules: [{required: true, message: '安措名称不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>
                                <a-form-item
                                        label="安措内容"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['content', {rules: [{required: true, message: '安措内容不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>
                                <a-form-item
                                        label="危险等级"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>
                                <a-form-item
                                        label="来源1：签发单2：现场补充"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>
                                <a-form-item
                                        label="状态1：待执行2：已执行3：已废弃"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>
                                <a-form-item
                                        label="备注"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['remark', {rules: [{required: true, message: '备注不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                                                                                                                </row-list>
                    </a-form>
        <template slot="footer">
            <a-button :loading="confirmLoading" type="primary" @click="safe()">保存</a-button>
        </template>
    </a-modal>
</template>

<script>
    import pick from 'lodash.pick'
    import {addTicketSafeExecute, updateTicketSafeExecute } from '@/api/ticket/ticketsafeexecute'

    export default {
        name: 'BaseTicketSafeExecute',
        data() {
            return {
                confirmLoading: false,
                modalTitle: null,
                form: this.$form.createForm(this),
                visible: false,
            // 下拉框map
                    }
        },
        props: {},
        created() {
            // 下拉框map
                    },
        methods: {
            base(record) {
                this.visible = true
                // 如果是空标识添加
                if (this.BaseTool.Object.isBlank(record)) {
                    this.modalTitle = '添加'
                    return
                }
                this.modalTitle = '编辑'
                const {form: {setFieldsValue}} = this
                // 日期处理
                                this.$nextTick(() => {
                    setFieldsValue(Object.assign(pick(record, [
                                                                                    'id',
                                                                                                                'ticketExcuteId',
                                                                                                                'type',
                                                                                                                'name',
                                                                                                                'content',
                                                                                                                'dangerLevel',
                                                                                                                'source',
                                                                                                                'status',
                                                                                                                'remark',
                                                                                                                                                                                                                ])))
                })
            },
            safe() {
                const {form: {validateFieldsAndScroll}} = this
                this.confirmLoading = true
                validateFieldsAndScroll((errors, values) => {
                    if (errors) {
                        this.confirmLoading = false
                        return
                    }
                    // 日期处理
                                        if (this.BaseTool.String.isBlank(values.id)) {
                        addTicketSafeExecute(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    } else {
                        updateTicketSafeExecute(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    }
                })
            },
            handleCancel(values) {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                if (this.BaseTool.Object.isNotBlank(values)) {
                    this.$emit('ok', values)
                }
            }

        }
    }
</script>
